package com.jsn.hello_cmake

import android.media.MediaCodec
import android.os.Build
import androidx.annotation.RequiresApi
import java.nio.ByteBuffer

abstract class BaseDecoder: Idecoder {
    //-------------线程相关------------------------
    /**
     * 解码器是否在运行
     *//*
    private var mIsRunning = true

    *//**
     * 线程等待锁
     *//*
    private val mLock = Object()

    *//**
     * 是否可以进入解码
     *//*
    private var mReadyForDecode = false

    //---------------解码相关-----------------------
    *//**
     * 音视频解码器
     *//*
    protected var mCodec: MediaCodec? = null

    *//**
     * 音视频数据读取器
     *//*
    protected var mExtractor: IExtractor? = null

    *//**
     * 解码输入缓存区
     *//*
    protected var mInputBuffers: Array<ByteBuffer>? = null

    *//**
     * 解码输出缓存区
     *//*
    protected var mOutputBuffers: Array<ByteBuffer>? = null

    *//**
     * 解码数据信息
     *//*
    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
    private var mBufferInfo = MediaCodec.BufferInfo()

    private var mState = DecodeState.STOP

    private var mStateListener: IDecoderStateListener? = null

    *//**
     * 流数据是否结束
     *//*
    private var mIsEOS = false

    protected var mVideoWidth = 0

    protected var mVideoHeight = 0
    //省略后面的方法

    final override fun run() {
        mState = DecodeState.START
        mStateListener?.decoderPrepare(this)

        //【解码步骤：1. 初始化，并启动解码器】
        if (!init()) return

        while (mIsRunning) {
            if (mState != DecodeState.START &&
                mState != DecodeState.DECODING &&
                mState != DecodeState.SEEKING) {
                waitDecode()
            }

            if (!mIsRunning ||
                mState == DecodeState.STOP) {
                mIsRunning = false
                break
            }

            //如果数据没有解码完毕，将数据推入解码器解码
            if (!mIsEOS) {
                //【解码步骤：2. 将数据压入解码器输入缓冲】
                mIsEOS = pushBufferToDecoder()
            }

            //【解码步骤：3. 将解码好的数据从缓冲区拉取出来】
           // val index = pullBufferFromDecoder()
         //   if (index >= 0) {
                //【解码步骤：4. 渲染】
          //      render(mOutputBuffers!![index], mBufferInfo)
                //【解码步骤：5. 释放输出缓冲】
//              //  if (mState == DecodeState.START) {
              //      mState = DecodeState.PAUSE
              //  }
            }
            //【解码步骤：6. 判断解码是否完成】
          //  if (mBufferInfo.flags == MediaCodec.BUFFER_FLAG_END_OF_STREAM) {
            //    mState = DecodeState.FINISH
                //mStateListener?.decoderFinish(this)
          //  }
        }
       // doneDecode()
        //【解码步骤：7. 释放解码器】
        //release()
    }


    *//**
     * 解码线程进入等待
     *//*
    private fun waitDecode() {
        //try {
         //   if (mState == DecodeState.PAUSE) {
               // mStateListener?.decoderPause(this)
         //   }
        //    synchronized(mLock) {
         //       mLock.wait()
          //  }
       // } catch (e: Exception) {
       //     e.printStackTrace()
      //  }
  //  }

    *//**
     * 通知解码线程继续运行
     *//*
    protected fun notifyDecode() {
        synchronized(mLock) {
            mLock.notifyAll()
        }
        if (mState == DecodeState.DECODING) {
            //mStateListener?.decoderRunning(this)
        }
    }

    *//**
     * 渲染
     *//*
    abstract fun render(outputBuffers: ByteBuffer,
                        bufferInfo: MediaCodec.BufferInfo)

    *//**
     * 结束解码
     *//*
    abstract fun doneDecode()*/

}